home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libcc / todo.cc < prev   
C/C++ Source or Header  |  1997-05-23  |  5KB  |  218 lines

  1.  
  2. #include "pi-source.h"
  3. #include <string.h>
  4. #include "pi-todo.h"
  5.  
  6. todoAppInfo_t::todoAppInfo_t(void *ap) 
  7.      : appInfo_t(ap)
  8. {
  9.      unsigned char *ptr = ((unsigned char *) ap) + BASE_APP_INFO_SIZE;
  10.      
  11.      _dirty = get_short(ptr);
  12.      
  13.      ptr += 2;
  14.      _sortByPriority = get_byte(ptr);
  15. }
  16.  
  17. void *todoAppInfo_t::pack(void) 
  18. {
  19.      unsigned char *buffer = new unsigned char [TODO_APP_INFO_SIZE];
  20.  
  21.      baseAppInfoPack(buffer);
  22.  
  23.      unsigned char *ptr = buffer + BASE_APP_INFO_SIZE;
  24.  
  25.      set_short(ptr, _dirty);
  26.  
  27.      ptr += 2;
  28.      set_short(ptr, _sortByPriority);
  29.  
  30.      return buffer;
  31. }
  32.  
  33. todo_t::todo_t(const todo_t &oldCopy) 
  34. {
  35.      (void) memcpy(this, &oldCopy, sizeof(todo_t));
  36.  
  37.      int len;
  38.  
  39.      if (oldCopy._due) {
  40.       _due = new tm;
  41.       (void) memcpy(_due, oldCopy._due, sizeof(tm));
  42.      }
  43.  
  44.      if (oldCopy._description) {
  45.       len = strlen(oldCopy._description);
  46.       _description = new char [len + 1];
  47.       (void) strcpy(_description, oldCopy._description);
  48.      }
  49.  
  50.      if (oldCopy._note) {
  51.       len = strlen(oldCopy._note);
  52.       _note = new char [len + 1];
  53.       (void) strcpy(_note, oldCopy._note);
  54.      }
  55. }
  56.  
  57. void todo_t::unpack(void *buf, int firstTime) 
  58. {
  59.      // If we unpack more than once, we need to free up any old data first
  60.      // so that we don't leak memory
  61.      if (!firstTime) {
  62.       if (_due)
  63.            delete _due;
  64.       if (_description)
  65.            delete _description;
  66.       if (_note)
  67.            delete _note;
  68.      }
  69.       
  70.      unsigned short d = get_short(buf);
  71.      if (d != 0xffff) {
  72.       _due = new tm;
  73.  
  74.       _due->tm_year = (d >> 9) + 4;
  75.       _due->tm_mon = ((d >> 5) & 15) - 1;
  76.       _due->tm_mday = d & 31;
  77.       _due->tm_hour = 0;
  78.       _due->tm_min = 0;
  79.       _due->tm_sec = 0;
  80.       _due->tm_isdst = -1;
  81.  
  82.       (void) mktime(_due);
  83.      } else
  84.       _due = NULL;
  85.  
  86.      unsigned char *ptr = ((unsigned char *)buf) + 2;
  87.      _priority = get_byte(ptr);
  88.  
  89.      ptr++;
  90.      if (_priority & 0x80) {
  91.       _complete = 1;
  92.       _priority &= 0x7f;
  93.      } else
  94.       _complete = 0;
  95.  
  96.      int len = strlen((const char *) ptr);
  97.      if (len) {
  98.       _description = new char [len + 1];
  99.       (void) strcpy(_description, (const char *) ptr);
  100.      } else
  101.       _description = NULL;
  102.      
  103.      ptr += len + 1;
  104.      if (*ptr != '\0') {
  105.       _note = new char [strlen((const char *) ptr) + 1];
  106.       (void) strcpy(_note, (const char *) ptr);
  107.      } else
  108.       _note = NULL;
  109. }
  110.  
  111. void *todo_t::internalPack(unsigned char *buffer) 
  112. {
  113.      if (_due)
  114.       setBufTm(buffer, _due);
  115.      else
  116.       *buffer = *(buffer + 1) = 0xff;
  117.      
  118.      unsigned char *ptr = buffer + 2;
  119.      
  120.      *ptr = (unsigned char) _priority;
  121.      if (_complete)
  122.       *ptr |= 0x80;
  123.  
  124.      ptr++;
  125.      if (_description) {
  126.       (void) strcpy((char *) ptr, _description);
  127.       ptr += strlen(_description) + 1;
  128.      } else
  129.       *ptr++ = '\0';
  130.  
  131.      if (_note)
  132.       (void) strcpy((char *) ptr, _note);
  133.      else
  134.       *ptr = '\0';
  135.  
  136.      return buffer;
  137. }
  138.  
  139. // Returns a pointer to the packed buffer, and set len to be the length of that
  140. // "string"
  141. void *todo_t::pack(int *len) 
  142. {
  143.      // 2 for the time, 1 for the priority
  144.      *len = 3;
  145.      if (_note)
  146.       *len += strlen(_note);
  147.  
  148.      // There is a null byte, whether or not there is a note
  149.      ++(*len);
  150.      
  151.      if (_description)
  152.       *len += strlen(_description);
  153.  
  154.      // There is a null byte, whether or not there is a description
  155.      ++(*len);
  156.      
  157.      unsigned char *ret = new unsigned char [*len];
  158.  
  159.      return internalPack(ret);
  160. }
  161.  
  162. // If the length passed in is not large enough to hold the packed data, a NULL
  163. // is returned.  Else, the buffer passed in is len bytes long.  Fill it in,
  164. // and then reset the length to the length of the buffer returned
  165. void *todo_t::pack(void *buffer, int *len) 
  166. {
  167.      // 2 for the time, 1 for the priority
  168.      int totalLength = 3;
  169.  
  170.      if (_note)
  171.       totalLength += strlen(_note);
  172.  
  173.      // There is a null byte, whether or not there is a note
  174.      totalLength++;
  175.      
  176.      if (_description)
  177.       totalLength += strlen(_description);
  178.      
  179.      // There is a null byte, whether or not there is a description
  180.      totalLength++;
  181.      
  182.      if (*len < totalLength)
  183.       return NULL;
  184.  
  185.      *len = totalLength;
  186.  
  187.      return internalPack((unsigned char *) buffer);
  188. }
  189.  
  190. // We can't just point to the data, as it might be deleted.  Make a copy
  191. void todoList_t::merge(todo_t &todo) 
  192. {
  193.      todo._next = _head;
  194.      _head = new todo_t(todo);
  195. }
  196.  
  197. // We can't just point to the data in the list, as it might get deleted on
  198. // us. We need to make a real copy
  199. void todoList_t::merge(todoList_t &list) 
  200. {
  201.      todo_t *newguy;
  202.  
  203.      for (todo_t *ptr = list._head; ptr != NULL; ptr = ptr->_next) {
  204.           newguy = new todo_t(ptr);
  205.           newguy->_next = _head;
  206.           _head = newguy;
  207.      }
  208. }
  209.  
  210. todoList_t::~todoList_t(void) {
  211.      todo_t *ptr, *next;
  212.  
  213.      for (ptr = _head; ptr != NULL; ptr = next) {
  214.           next = ptr->_next;
  215.           delete ptr;
  216.      }
  217. }
  218.